#include "MyMap.h"
#include "common.h"

#define MaxMap 60
using namespace std;

typedef pair<string,int> pair_map;
vector<pair_map> tVector;

int InsertMap(char *str,map<string,int>mapword[],int index)
{
	pair<map<string,int>::iterator,bool>Insert_pair;
	Insert_pair = mapword[index].insert(pair_map(str,0));//first insert ,the count needs not statistics

	if (Insert_pair.second == true)
	{
		return 1;
	}	
	else
	{
		return 0;
	}
}

int InsertMapStr(char *str,map<string,int>&mailmap)
{
	pair<map<string,int>::iterator,bool>Insert_pair;
	Insert_pair = mailmap.insert(pair_map(str,1));//first insert ,the count needs statistics

	if (Insert_pair.second == true)
	{
		return 1;
	}	
	else
	{
		return 0;
	}
}

void FindMapWord(char *str,map<string,int>mapword[],int index)
{
	map<string,int>::iterator iter;
	iter = mapword[index].find(str);
	if (iter != mapword[index].end())   //if the word exists in mapword ,then do nothing
	{
		;
	} 
	else        //if the word does not exist in the mapword,then insert the mapword[index]
	{
		if (InsertMap(str,mapword,index) == 1)
		{
			return ;
		} 
		else
		{
			exit(-1);
		}	
	}
}

void FindMapWordStr(char *str,map<string,int>&mailmap)
{
	map<string,int>::iterator iter;
	iter = mailmap.find(str);
	if (iter != mailmap.end())   //the str had existed in the mapword
	{
		//cout<<"Find "<<str<<",the count is "<<iter->second<<endl;
		++(iter->second);
	} 
	else
	{
		//cout<<"Do not find"<<endl;
		if (InsertMapStr(str,mailmap) == 1)
		{
			return ;
		} 
		else
		{
			exit(-1);
		}	
	}
}

int FindWordNumsLib(char *word,map<string,int>maillib)
{
	map<string,int>::iterator iter;
	iter = maillib.find(word);
	if(iter != maillib.end())  //the word had existed in the maillib
	{
		return (iter->second);  //return the num 
	}
	else
		return 0;
}


void MapWordSize(int index,map<string,int>mapword[])
{
	cout<<"mapsize="<<mapword[index].size()<<endl;
}

int MapWordSizeStr(map<string,int>&mailmap)
{
	//cout<<"mapsize="<<mailmap.size()<<endl;
	return mailmap.size();
}

void PrintfMap(int filecounts,map<string,int>mapword[])
{
	int index;
	map <string,int> ::iterator iter;
	cout<<"mapword order as following"<<endl;
	for(index=0;index<filecounts;index++)
	{
		cout<<"the "<<index<<" map wordmap"<<endl;
		for(iter = mapword[index].begin();iter!=mapword[index].end();iter++)
		{	
			cout<<iter->first<<":"<<iter->second<<endl;
		}
	}
	
}

void ClearMapWord(int index,map<string,int>mapword[])
{
	mapword[index].clear();
}

void DeleteMMap(int N,map<string,int>mapword[])
{
	
	int index;
	for(index = 0;index < N ;index++)
	{
		mapword[index].clear();
	}
}

void StatisticsMapWords(map<string,int> &result_map,int N,map<string,int>mapword[])
{
	int index;
	map <string,int>::iterator iter;
	map <string,int>::iterator iter2;
	for(index=0;index<N;index++)
	{
		for(iter = mapword[index].begin();iter!=mapword[index].end();iter++)
		{
			iter2 = result_map.find(iter->first);
			if(iter2 != result_map.end())//if the iter ->first exists in spam_map,then increase the counts
			{
				++(iter2->second);
			}
			else//if the iter->first does exirst in spam_map,then insert the spam_map 
			{
				result_map.insert(pair_map(iter->first,1));
			}
		}
	
	}
}

void PrintfResult_map(map<string,int> &result_map)
{
	int index;
	map <string,int> ::iterator iter;
	cout<<"result_map order as following"<<endl;
	for(iter = result_map.begin();iter!=result_map.end();iter++)
	{	
		cout<<iter->first<<":"<<iter->second<<endl;
	}
}

int cmp(const pair<string,int>& x,const pair<string,int>& y)
{
	return x.second < y.second;
}

void sortMapByValue(map<string,int>& mailmap,vector< pair<string,int> >& tVector)
{
	for(map<string,int>::iterator iter=mailmap.begin();iter!=mailmap.end();iter++)
	{
		tVector.push_back(make_pair(iter->first,iter->second));
	}
	sort(tVector.begin(),tVector.end(),cmp);

	//for test 
#if 0
	cout<<"tvector start"<<endl;
	for(int i=0;i<tVector.size();i++)  
	{  
		cout<<tVector[i].first<<":"<<tVector[i].second<<endl;  
	} 
	cout<<"tvector end"<<endl;
#endif
}

